//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CPICapFloor.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Indexes/ZeroInflationIndex.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CCPICapFloor::CCPICapFloor (QL::Option::TypeEnum type, Double nominal, DateTime startDate, Double baseCPI, DateTime maturity, Cephei::QL::Times::ICalendar^ fixCalendar, QL::Times::BusinessDayConventionEnum fixConvention, Cephei::QL::Times::ICalendar^ payCalendar, QL::Times::BusinessDayConventionEnum payConvention, Double strike, Cephei::QL::Indexes::IZeroInflationIndex^ infIndex, Cephei::QL::Times::IPeriod^ observationLag, Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::CPI::InterpolationTypeEnum>^ observationInterpolation, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CCPICapFloor::typeid)
{
    CCalendar^ _CfixCalendar;
    CCalendar^ _CpayCalendar;
    CZeroInflationIndex^ _CinfIndex;
    CPeriod^ _CobservationLag;
    try
    {
#ifdef HANDLE
        _phCPICapFloor = NULL;
#endif
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal); //d
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //d
        QuantLib::Real _baseCPI = (QuantLib::Real)ValueHelper::Convert (baseCPI); //d
        QuantLib::Date _maturity = (QuantLib::Date)ValueHelper::Convert (maturity); //d
        _CfixCalendar = safe_cast<CCalendar^> (fixCalendar);
        _CfixCalendar->Lock();
        QuantLib::Calendar& _fixCalendar = static_cast<QuantLib::Calendar&> (_CfixCalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _fixConvention = (QuantLib::BusinessDayConvention)fixConvention ;
        _CpayCalendar = safe_cast<CCalendar^> (payCalendar);
        _CpayCalendar->Lock();
        QuantLib::Calendar& _payCalendar = static_cast<QuantLib::Calendar&> (_CpayCalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _payConvention = (QuantLib::BusinessDayConvention)payConvention ;
        QuantLib::Rate _strike = (QuantLib::Rate)ValueHelper::Convert (strike); //d
        _CinfIndex = safe_cast<CZeroInflationIndex^> (infIndex);
        _CinfIndex->Lock();
        Handle<QuantLib::ZeroInflationIndex>& _infIndex = static_cast<Handle<QuantLib::ZeroInflationIndex>&> (_CinfIndex->GetHandle ()); 
        _CobservationLag = safe_cast<CPeriod^> (observationLag);
        _CobservationLag->Lock();
        QuantLib::Period& _observationLag = static_cast<QuantLib::Period&> (_CobservationLag->GetReference ()); 
        QuantLib::CPI::InterpolationType _observationInterpolation = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::CPI::InterpolationTypeEnum>::IsSome::get (observationInterpolation) ? (QuantLib::CPI::InterpolationType)observationInterpolation->Value : QuantLib::CPI::InterpolationType::AsIndex); //10
        _ppCPICapFloor = new boost::shared_ptr<QuantLib::CPICapFloor> (new QuantLib::CPICapFloor ( _type,  _nominal,  _startDate,  _baseCPI,  _maturity,  _fixCalendar,  _fixConvention,  _payCalendar,  _payConvention,  _strike,  _infIndex,  _observationLag,  _observationInterpolation ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCPICapFloor)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCPICapFloor));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfixCalendar != nullptr) _CfixCalendar->Unlock();
        if (_CpayCalendar != nullptr) _CpayCalendar->Unlock();
        if (_CinfIndex != nullptr) _CinfIndex->Unlock();
        if (_CobservationLag != nullptr) _CobservationLag->Unlock();
    }
}
Cephei::QL::Instruments::CCPICapFloor::CCPICapFloor (boost::shared_ptr<QuantLib::CPICapFloor>& childNative, Object^ owner) : CInstrument(CCPICapFloor::typeid)
{
#ifdef HANDLE
	_phCPICapFloor = NULL;
#endif
	_ppCPICapFloor = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCPICapFloor));
}
Cephei::QL::Instruments::CCPICapFloor::CCPICapFloor (QuantLib::CPICapFloor& childNative, Object^ owner) : CInstrument(CCPICapFloor::typeid)
{
#ifdef HANDLE
	_phCPICapFloor = NULL;
#endif
	_ppCPICapFloor = new boost::shared_ptr<QuantLib::CPICapFloor> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCPICapFloor));
    _CPICapFloorOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Instruments::CCPICapFloor::CCPICapFloor (CCPICapFloor^ copy) : CInstrument(CCPICapFloor::typeid)
{
#ifdef HANDLE
	_phCPICapFloor = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCPICapFloor = new boost::shared_ptr<QuantLib::CPICapFloor> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCPICapFloor));
    }
}
Cephei::QL::Instruments::CCPICapFloor::CCPICapFloor (PLATFORM::Type^ t) : CInstrument(CCPICapFloor::typeid)
{
#ifdef HANDLE
	_phCPICapFloor = NULL;
#endif
	if (!t->IsSubclassOf(CCPICapFloor::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CCPICapFloor::CCPICapFloor (QuantLib::Handle<QuantLib::CPICapFloor>& childNative, Object^ owner)  : CInstrument(CCPICapFloor::typeid)
{
	_phCPICapFloor = &childNative;
	_ppCPICapFloor = &static_cast<boost::shared_ptr<QuantLib::CPICapFloor>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCPICapFloor));
    _CPICapFloorOwner = owner;
}
Cephei::QL::Instruments::CCPICapFloor::CCPICapFloor (QuantLib::Handle<QuantLib::CPICapFloor> childNative)  : CInstrument(CCPICapFloor::typeid)
{
	_phCPICapFloor = &childNative;
	_ppCPICapFloor = &static_cast<boost::shared_ptr<QuantLib::CPICapFloor>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCPICapFloor));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CCPICapFloor::CCPICapFloor (QuantLib::CPICapFloor childNative)  : CInstrument(CCPICapFloor::typeid)
{
#ifdef HANDLE
	_phCPICapFloor = NULL;
#endif
	_ppCPICapFloor = new boost::shared_ptr<QuantLib::CPICapFloor> (new QuantLib::CPICapFloor (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCPICapFloor));
}
#endif

Cephei::QL::Instruments::CCPICapFloor::~CCPICapFloor ()
{
    if (_ppCPICapFloor != NULL)
    {
	    delete _ppCPICapFloor;
        _ppCPICapFloor = NULL;
    }
}
Cephei::QL::Instruments::CCPICapFloor::!CCPICapFloor ()
{
    if (_ppCPICapFloor != NULL)
    {
	    delete _ppCPICapFloor;
    }
}
QuantLib::CPICapFloor& Cephei::QL::Instruments::CCPICapFloor::GetReference ()
{
    if (_ppCPICapFloor == NULL) throw REFNEW NativeNullException ();
	return **_ppCPICapFloor;
}
boost::shared_ptr<QuantLib::CPICapFloor>& Cephei::QL::Instruments::CCPICapFloor::GetShared ()
{
    if (_ppCPICapFloor == NULL) throw REFNEW NativeNullException ();
	return *_ppCPICapFloor;
}
QuantLib::CPICapFloor* Cephei::QL::Instruments::CCPICapFloor::GetPointer ()
{
    if (_ppCPICapFloor == NULL) throw REFNEW NativeNullException ();
	return &**_ppCPICapFloor;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CPICapFloor>& Cephei::QL::Instruments::CCPICapFloor::GetHandle ()
{
	if (_phCPICapFloor == NULL)
	{
		_phCPICapFloor = new Handle<QuantLib::CPICapFloor> (*_ppCPICapFloor);
	}
	return *_phCPICapFloor;
}
#endif
bool Cephei::QL::Instruments::CCPICapFloor::HasNative () 
{
	return (_ppCPICapFloor != NULL);
}

DateTime Cephei::QL::Instruments::CCPICapFloor::FixingDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCPICapFloor)->fixingDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IZeroInflationIndex^ Cephei::QL::Instruments::CCPICapFloor::InflationIndex::get ()
{
    try
    {
    	Handle<QuantLib::ZeroInflationIndex> _rv = (Handle<QuantLib::ZeroInflationIndex>)(*_ppCPICapFloor)->inflationIndex ( );   
        Cephei::QL::Indexes::CZeroInflationIndex^ _nrv = REFNEW Cephei::QL::Indexes::CZeroInflationIndex (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Instruments::CCPICapFloor::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCPICapFloor)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCPICapFloor::Nominal::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCPICapFloor)->nominal ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Instruments::CCPICapFloor::ObservationLag::get ()
{
    try
    {
    	QuantLib::Period _rv = (QuantLib::Period)(*_ppCPICapFloor)->observationLag ( );   
        Cephei::QL::Times::CPeriod^ _nrv = REFNEW Cephei::QL::Times::CPeriod (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CCPICapFloor::PayDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCPICapFloor)->payDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCPICapFloor::Strike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCPICapFloor)->strike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Option::TypeEnum Cephei::QL::Instruments::CCPICapFloor::Type::get ()
{
    try
    {
    	QuantLib::Option::Type _rv = (QuantLib::Option::Type)(*_ppCPICapFloor)->type ( );   
        QL::Option::TypeEnum _nrv = (QL::Option::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ICPICapFloor^ Cephei::QL::Instruments::CCPICapFloor_Factory::Create (QL::Option::TypeEnum type, Double nominal, DateTime startDate, Double baseCPI, DateTime maturity, Cephei::QL::Times::ICalendar^ fixCalendar, QL::Times::BusinessDayConventionEnum fixConvention, Cephei::QL::Times::ICalendar^ payCalendar, QL::Times::BusinessDayConventionEnum payConvention, Double strike, Cephei::QL::Indexes::IZeroInflationIndex^ infIndex, Cephei::QL::Times::IPeriod^ observationLag, Microsoft::FSharp::Core::FSharpOption<QL::Cashflows::CPI::InterpolationTypeEnum>^ observationInterpolation, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CCPICapFloor ( type,  nominal,  startDate,  baseCPI,  maturity,  fixCalendar,  fixConvention,  payCalendar,  payConvention,  strike,  infIndex,  observationLag,  observationInterpolation,  QL_Pricer);
}
